{
  /*合同管理 createBy 2017-06-07*/
}
import React from "react";
import Relay from "react-relay";
import { message, Radio } from 'antd';
import { Button } from 'fly';
import './css/contract_management.css';
import Util from "../../../../../util/Util";
import SignModal from "../../SignModal";
import InfoOrderMutation from '../../../mutation/InfoOrderMutation';
import InfoEnquiryOrderMutation from '../../../mutation/InfoEnquiryOrderMutation';
import SignOrderMutation from "../../mutation/SignOrderMutation";
import OrderdetailContractInfo from '../../OrderdetailContractInfo';
import SupplementaryContract from './SupplementaryContract';

const RadioGroup = Radio.Group;

export default class A extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            orderOperates:[],
            order: [],
            value: -1,
            partBId: '',
            _orderstatus: '',
        };
        this.enterpriseId = sessionStorage.getItem('enterpriseId');
        this.orderId = this.props.orderId;
        this.orderstatus = this.props.orderstatus;
        // this.signOrder = this.signOrder
        message.config({
          top: 30,
          duration: 2
        })
    }

    componentWillMount() {
      if(this.props.enquiryId !='' && this.props.enquiryId != undefined) {
          this.EnquiryOrderdetail();
      } else {
          this.InfoOrderDetail();
      }
    }
    EnquiryOrderdetail() {
      /*infoorder接口*/
      Relay.Store.commitUpdate(
          new InfoEnquiryOrderMutation({
              orderId: this.orderId,
              enId: this.enterpriseId,
              enquiryId: this.props.enquiryId
          }),
          {
              onFailure: () => {
                  message.info('请求失败')
                  this.setState({
                    isLoading: false
                  })
              },
              onSuccess: (response) => {
                  let errorCode = response.infoEnquiryOrder.infoEnquiryOrder.errorCode;
                  if(errorCode=='0'){
                    let order = response.infoEnquiryOrder.infoEnquiryOrder.order;
                    if (order && order.orderOperates) {
                      this.setState({
                          orderOperates: order.orderOperates,
                          order: order,
                      },()=>{
                        if(this.refs.ordercinfo){
                          this.refs.ordercinfo.setState({
                            supplyContractsData : order.supplyContracts
                          });
                        }
                      })

                    }
                  } else {
                    message.info('请求失败')
                  }
              }
          }
      )
    }

    InfoOrderDetail() {
      /*infoorder接口*/
      Relay.Store.commitUpdate(
          new InfoOrderMutation({
              orderId: this.orderId,
              enId: this.enterpriseId,
          }),
          {
              onFailure: () => {
                  message.info('请求失败')
                  this.setState({
                    isLoading: false
                  })
              },
              onSuccess: (response) => {
                  let errorCode = response.infoOrder.infoOrder.errorCode;
                  if(errorCode=='0'){
                    let order = response.infoOrder.infoOrder.order;
                    if (order && order.orderOperates) {
                      this.setState({
                        orderOperates: order.orderOperates,
                        order: order
                      },()=>{
                        if(this.refs.ordercinfo){
                          this.refs.ordercinfo.setState({
                            supplyContractsData : order.supplyContracts
                          });
                        }
                      })

                    }
                  } else {
                    message.info('请求失败')
                  }
              }
          }
      )
    }

    /*点击签订*/
    signOrder(){
      if (this.state.value < 0) {
        message.info('请选择企业');
        return;
      } else {
          this.refs.signmodal.setState({
              visible:true
          });
      }
    }

    SingOrderCallBack(radioValue, tnum,instructions){
        Relay.Store.commitUpdate(
            new SignOrderMutation({
                orderId: this.orderId,
                enId: this.state.partBId,
                signedContract: tnum,
                signedContractUrl1: instructions[0]||'',
                signedContractUrl2: instructions[1]||'',
                signedContractUrl3: instructions[2]||'',
                signedContractUrl4: instructions[3]||'',
                signedContractUrl5: instructions[4]||'',
                isOnlineBalance: radioValue,
                enquiryId: this.props.enquiryId
            }),
            {
                onFailure: () => {
                    message.error('请求失败');
                },
                onSuccess: (response) => {
                    let errorCode = response.signOrder.signOrder.errorCode;
                    let value = response.signOrder.signOrder.value;
                    if(errorCode=='0'){
                        message.success('签订成功');
                        let order = response.signOrder.signOrder.order;
                        this.order = order;
                        this.setState({
                            orderstatus:order.status,
                        });
                        this.refs.signmodal.setState({
                            visible: false
                        });
                        this.InfoOrderDetail()
                        this.props.orderDetail()
                    } else {
                        message.config({
                            top: 30,
                        })
                        message.error(value)
                    }
                }
            }
        )
    }

    radioChange = (e) => {
      let toId = this.state.orderOperates[e.target.value - 1].toId;
      let _orderstatus = this.state.orderOperates[e.target.value - 1].status;
      this.setState({
          partBId: toId,
          value: e.target.value,
          _orderstatus: _orderstatus
      })
    }

    render() {
        const { orderOperates, order } = this.state;
        const radioStyle = {
            display: 'block',
            height: '38px',
            lineHeight: '38px',
        };
        return(
          <div>
              {
                order.status == 1 || order.status == 2 ? (
                  <div>
                    <p className="contract_management_title">选择签订的供应方</p>
                    <div className="contract_management_box">
                      <RadioGroup onChange={this.radioChange} value={this.state.value}>
                        {
                          orderOperates ? orderOperates.map((v,i)=>{
                              {
                                 return  v.status >= '2' ? (
                                      <Radio style={radioStyle} value={i+1}>
                                          <span className="contract_management_companyname">{v.toUserName}</span>
                                          <span className="contract_management_companystatus">已接单</span>
                                      </Radio>
                                  ): (
                                      <Radio disabled style={radioStyle} value={i+1}>
                                          <span className="contract_management_companyname">{v.toUserName}</span>
                                          <span className="contract_management_companystatus">未接单</span>
                                      </Radio>
                                  )
                              }

                          }) : null
                        }
                      </RadioGroup>
                    </div>
                    <div className="contract_management_sign">
                      <Button onClick={this.signOrder.bind(this)}>签订</Button>
                    </div>
                    <SignModal ref={'signmodal'}  SingOrderCallBack = {this.SingOrderCallBack.bind(this)} />
                  </div>
                ) : (
                  <div>
                    <SupplementaryContract order={this.state.order} />
                    {
                      this.state.order && this.state.order.status >= 3 ? (
                        <OrderdetailContractInfo order={this.state.order} Orderdetail={this.InfoOrderDetail.bind(this)} ref={'ordercinfo'}/>
                      ) : null
                    }
                  </div>
                )
              }
          </div>
        )
    }
}
